#include "cdialog.h"

int CDialog::maxZLevel = 0;

/**
 * CDialog - constructor, creates dialog
 * @param layoutPtr
 * @param x
 * @param y
 */
CDialog::CDialog(const CLayout &layout, int x, int y) {
  this->init();
  this->x = x;
  this->y = y;
  this->z = maxZLevel++;
  this->layout = layout;
  this->width = this->layout.calculateTotalWidth();
  this->height = this->layout.calculateTotalHeight();
}
//---------------------------------------------------------------------------

/**
 * getZ - gets z-position of component
 * @return component z-position
 */
int CDialog::getZ() const {
  return z;
}
//---------------------------------------------------------------------------

/**
 * setX - sets component position on x axis
 * @param x
 */
void CDialog::setX(int x) {
  CGuiObject::setX(x);
}
//---------------------------------------------------------------------------

/**
 * setY - sets component position on y axis
 * @param y
 */
void CDialog::setY(int y) {
  CGuiObject::setY(y);
}
//---------------------------------------------------------------------------

/**
 * setZ - sets component position on z axis
 * @param z
 */
void CDialog::setZ(int z) {
  this->z = z;
}
//---------------------------------------------------------------------------

/**
 * addItem - adds component on dialog
 * @param itemPtr
 * @param posX
 * @param posY
 * @param spanX
 * @param spanY
 */
void CDialog::addItem(CGuiObject* itemPtr, TIndex posX, TIndex posY,
    TIndex spanX, TIndex spanY) {
  CGuiObject::addItem(itemPtr, posX, posY, spanX, spanY);
}
//---------------------------------------------------------------------------

/**
 * isFocused - determinates if cursor is located above component
 * @param eventPtr
 * @return true/false cursor is/is not above component
 */
bool CDialog::isFocused(CMouseEvent* eventPtr) {
  if (eventPtr->getMouseX() >= x && eventPtr->getMouseY() >= y
      && eventPtr->getMouseX() <= x + width && eventPtr->getMouseY() <= y
      + height) {
    return true;
  } else {
    vector<CLayoutItem*>::iterator i = items->begin();
    for (; i < items->end(); i++) {
      CRect<int> tmp;
      if ((*i)->getComponentPtr()->isOverlap(tmp)) {
        if (eventPtr->getMouseX() >= tmp.getLeft() && eventPtr->getMouseY()
            >= tmp.getTop() && eventPtr->getMouseX() <= tmp.getRight()
            && eventPtr->getMouseY() <= tmp.getBottom()) {
          return true;
        }
      }
    }
  }
  return false;
}
//---------------------------------------------------------------------------

/**
 * isOverlap - returns true if any component overlaps dialog dimensions
 * @param rect
 * @return true/false some component does/does not overlap dialog dimensions
 */
bool CDialog::isOverlap(CRect<int> &rect) {
  vector<CLayoutItem*>::iterator i = items->begin();
  for (; i < items->end(); i++) {
    CRect<int> tmp;
    if ((*i)->getComponentPtr()->isOverlap(tmp)) {
      return true;
    }
  }
  return false;
}
//---------------------------------------------------------------------------

/**
 * drawSelf - draw dialog
 * @param rendererPtr
 */
void CDialog::drawSelf(CGuiRenderer* rendererPtr) {
}
//---------------------------------------------------------------------------
